package rsj.proxy.strategy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import rsj.annotation.Update;
import rsj.proxy.PreExecuteStatementProcessor;
import rsj.proxy.parse.SqlSource;
import rsj.proxy.parse.SqlSourceFactory;

import java.lang.reflect.Method;
import java.sql.Connection;

/**
 * 增删改执行逻辑
 *
 * @author yuanqinglong
 * @since 2021/5/8 10:13
 */
@Component
public class UpdateAnnotationStrategy implements IExecuteStrategy {

    private static final Logger logger = LoggerFactory.getLogger(UpdateAnnotationStrategy.class);


    @Override
    public Class<?> getSupportAnnotationType() {
        return Update.class;
    }



    @Override
    public Object executeStrategy(Connection connection, Method method, Object[] args) throws Exception {
        SqlSource sqlSource = SqlSourceFactory.getSqlSource(method);
        Object[] params = this.paramResolve(sqlSource, method, args);
        PreExecuteStatementProcessor executeProcessor = this.getExecuteProcessor(method);
        logger.info("执行SQL: \n\n{} \n参数:{}", sqlSource.getFinalSql(),params);
        return executeProcessor.execute(connection, sqlSource.getFinalSql(), params);
    }
}
